package liurong.driver.zk;

import java.util.Scanner;

import org.I0Itec.zkclient.ZkClient;

public class ServerRegister {
	public static final String ROOT_LIURONG = "/root_liurong";

	public static void main(String[] args) {
		ZkClient zkClient = new ZkClient("127.0.0.1:2181", 30000, Integer.MAX_VALUE);

		if (!zkClient.exists(ROOT_LIURONG)) {
			zkClient.createPersistent(ROOT_LIURONG);
		}

		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			String next = scanner.next();
			System.out.println("输入为：" + next);
			if (next == null) {
				continue;
			}
			String[] split = next.split("[-]");
			if (split.length < 1) {
				continue;
			}
			String childPath = ROOT_LIURONG + "/" + split[1];
			// 注册
			switch (split[0].toLowerCase()) {
			case "add":
				zkClient.createEphemeral(childPath);
				break;
			case "create":
				zkClient.createEphemeral(childPath);
				break;
			case "del":
				zkClient.delete(childPath);
				break;
			case "delete":
				zkClient.delete(childPath);
				break;
			default:
				break;
			}
		}

		scanner.close();
	}
}
